window.operateEvents = {
    'click .check': function (e, value) {
        var info = bytes2Str(value);
        window.wxc.xcConfirm(info, window.wxc.xcConfirm.typeEnum.info);
    },
    'click .download': function (e, value, row) {
        var row_data = row;
        var table = $('#table_input').val();
        var column_name;
        var index = $('td').index($(e.target).parent());
        var column_number = calculate_column($('#table'));
        var remainder = index % column_number;
        switch (table) {
            case "1"://加密
                column_name = get_column_name(remainder, column_number);
                break;
            case "2"://解密
                column_name = get_column_name(remainder, column_number);
                break;
            case "3"://验证
                if (remainder === column_number - 4) {
                    column_name = "public_key";
                } else if (remainder === column_number - 3) {
                    column_name = "message";
                } else {
                    column_name = "";
                }
                break;
            case "4"://签名
                if (remainder === column_number - 3) {
                    column_name = "private_key";
                } else if (remainder === column_number - 2) {
                    column_name = "message";
                } else {
                    column_name = "";
                }
                break;
            case "5"://签密
                if (remainder === column_number - 5) {
                    column_name = "encrypt_key";
                } else if (remainder === column_number - 4) {
                    column_name = "sign_key";
                } else if (remainder === column_number - 3) {
                    column_name = "plaintext";
                } else if (remainder === column_number - 2) {
                    column_name = "ciphertext";
                } else {
                    column_name = "";
                }
                break;
            case "6"://解签密
                if (remainder === column_number - 6) {
                    column_name = "decrypt_key";
                } else if (remainder === column_number - 5) {
                    column_name = "verify_key";
                } else if (remainder === column_number - 4) {
                    column_name = "plaintext";
                } else if (remainder === column_number - 3) {
                    column_name = "ciphertext";
                } else {
                    column_name = "";
                }
                break;
            case "7"://MAC
                if (remainder === column_number - 3) {
                    column_name = "secret_key";
                } else if (remainder === column_number - 2) {
                    column_name = "message";
                } else if (remainder === column_number - 1) {
                    column_name = "authentication_code";
                } else {
                    column_name = "";
                }
                break;
            case "8"://认证加密
                column_name = get_column_name(remainder, column_number);
                break;
            case "9"://解密验证
                if (remainder === column_number - 4) {
                    column_name = "secret_key";
                } else if (remainder === column_number - 3) {
                    column_name = "message";
                } else if (remainder === column_number - 2) {
                    column_name = "authentication_code";
                } else {
                    column_name = "";
                }
        }

        if (column_name === "") {
            window.wxc.xcConfirm("下载定位出错", window.wxc.xcConfirm.typeEnum.error);
            return;
        }

        var form = $("<form>");
        form.attr("style", "display:none");
        form.attr("method", "post");
        form.attr("action", "servlet/DownloadServlet");
        var input_id = $("<input>");
        input_id.attr("type", "hidden");
        input_id.attr("name", "id");
        input_id.attr("value", row_data.id);
        var input_table = $("<input>");
        input_table.attr("type", "hidden");
        input_table.attr("name", "table");
        input_table.attr("value", table);
        var input_column = $("<input>");
        input_column.attr("type", "hidden");
        input_column.attr("name", "column");
        input_column.attr("value", column_name);
        $("body").append(form);
        form.append(input_id);
        form.append(input_table);
        form.append(input_column);
        form.submit();
    }
};

$(function () {
    $('#table_input').on('changed.bs.select', function () {
        query();
    });
});

function query() {
    var start_time = $('#dtp_start').val();
    var end_time = $('#dtp_end').val();
    var operator = $('#operator_input').val();
    var table = $('#table_input').val();

    if (start_time === "" || end_time === "") {
        window.wxc.xcConfirm("选择时间段", window.wxc.xcConfirm.typeEnum.error);
        return;
    }

    var columns = getColumns(table);
    var $table = $('#table');
    $table.bootstrapTable('destroy');
    $table.bootstrapTable({
        url: "servlet/QueryServlet",
        method: 'get',
        sortOrder: 'desc',
        cache: false,
        pagination: true,
        sidePagination: 'server',
        pageNumber: 1,
        pageSize: 10,
        pageList: [10, 20, 30],
        columns: columns,
        showRefresh: true,
        showToggle: true,
        showColumns: true,
        showExport: true,
        queryParams: function (params) {
            return {
                pageSize: params.limit,
                pageNumber: params.offset,
                sortOrder: params.order,
                start: start_time,
                end: end_time,
                operator: operator,
                table: table
            }
        },
        onLoadSuccess: function () {
        },
        onLoadError: function () {
            window.wxc.xcConfirm("数据查询失败", window.wxc.xcConfirm.typeEnum.error);
        }
    });
}

function calculate_column(table) {
    var td_number = table.find('td').length;
    var tr_number = table.find('tr').length - 1;//去掉标题行
    return td_number / tr_number;
}

function get_column_name(remainder, column_number) {
    var column_name;
    switch (remainder) {
        case column_number - 3:
            column_name = "secret_key";
            break;
        case column_number - 2:
            column_name = "plaintext";
            break;
        case column_number - 1:
            column_name = "ciphertext";
            break;
        default:
            return "";
    }
    return column_name;
}